<?php

class Zwei_Admin_Settings{
	public $page;
	
	public function __construct($page, $zend_view)
	{
    	$this->page=$page;
/*
		$zend_view->dojo()
    		->requireModule("dijit.layout.TabContainer")
			->requireModule("dijit.layout.ContentPane")
			->requireModule("dijit.Dialog")
			->requireModule("dijit.form.TextBox")
			->requireModule("dijit.form.Button")
			->requireModule("dijit.form.DateTextBox")
			->requireModule("dijit.form.FilteringSelect")
			->requireModule("dojox.grid.DataGrid")
			->requireModule("dojox.form.CheckedMultiSelect")
			->requireModule("dojo.data.ItemFileWriteStore")
			->requireModule("dojo.data.ItemFileReadStore")
			->requireModule("dojo.date.locale");
			
		$zend_view->headStyle()->appendStyle('
			  @import "/dojotoolkit/dojox/grid/resources/Grid.css";		
              @import "/dojotoolkit/dojox/grid/resources/claroGrid.css";
              @import "/dojotoolkit/dojox/form/resources/CheckedMultiSelect.css";              
        ');	
        */
  	}

	public function display()
	{
    	$form=new Zwei_Utils_Form();
		$settings=new SettingsModel();
	    $out = "<div class=\"claro\" style=\"height:100%;width:100%;\">\n<h2>Configuraci&oacute;n del Sitio</h2>\r\n";
	
	    if(isset($form->save)){
		    foreach ($form->id as $i=>$v){
			    $value=isset($form->value[$i])?$form->value[$i]:"";
			    $where=$settings->getAdapter()->quoteInto('id = ?', $v);
		    	$data=array('value'=>$value);
		    	$settings->update($data, $where);	    
	    	}
    	}

	    if(isset($form->f)&&isset($form->v)){
	    	if(class_exists($form->f)){
	    		$f=new $form->f;
	    		$f->setValue($form->v);
	    		return $f->run();
	    	}
	    }

		$out.="<form action=\"admin?p=$this->page\" method=\"post\"  style=\"height:100%;width:100%;\">\r\n";

	    $out.="<div dojoType=\"dijit.layout.TabContainer\" style=\"width: 100%; height: 100%;\" tabStrip=\"true\">\r\n";		
	
		$groupies=$settings->loadGroups();
		$groups=array();
		$h='style="background:#cccccc"';
		$i=0;
		foreach($groupies as $group){
			$g=!empty($group['group'])?$group['group']:'General';
			$groups[]=$group['group'];
			//$out.='<a class="settings_tab" '.$h.' onclick="showtab(this,\'tab'.++$i.'\')">'.$g.'</a>';
			$selected=($i==0) ? ' selected="true"':'';
			$out.="<div dojoType=\"dijit.layout.ContentPane\" title=\"{$group['group']}\" $selected>";
			
			//Subgrupos
			$settings=new SettingsModel();
			$query=
				$settings->select()
				->where($settings->getAdapter()->quoteInto('settings.group = ?', $group['group']))
				->order('ord ASC');
			
			
			$subgroups=$settings->fetchAll($query);
			foreach($subgroups as $sg){
				$out.="<input type=\"hidden\" name=\"id[]\" value=\"$sg[id]\" />\r\n";
				$out.="<b>$sg[id]:</b> ";
				$e=new $sg['type'](true,true,"","value",$sg['value']);
	
				if(!empty($sg['enum'])){
	
					$out.=$this->_select("value[$i]",$sg['enum'],$sg['value']);
	
				}else{
	
					$out.=$e->edit($i,0);
	
				}
	
				if(!empty($sg['function'])&&class_exists($sg['function'])){
	
					$f=new $sg['function'];
					$f->setId($i);
					$out.=$f->display();
				}
				$out.="<br />".nl2br($sg['description']);
				$out.="\r\n<br /><br />\r\n";
				$i++;

			}
			
			///Subgrupos
			
			$out.='</div>';
			
			$h='';
		}
		//$out.='<div class="brclear"></div><br />';
		//$settings=new SettingsModel();
		//$hidden='';
		//$i=0;
		//$j=0;

		/*
		foreach($groups as $group)
		{
			$query=
				$settings->select()
				->where($settings->getAdapter()->quoteInto('settings.group = ?', $group))
				->order('ord ASC');
			
			
			$groupies=$settings->fetchAll($query);
				
		    $out.='<div id="tab'.($j+1).'" class="settings_area" '.$hidden.'>';
		    $j++;
	
			foreach($groupies as $row){
				$out.="<input type=\"hidden\" name=\"id[]\" value=\"$row[id]\" />\r\n";
				$out.="<b>$row[id]:</b> ";
				$e=new $row['type'](true,true,"","value",$row['value']);
	
				if(!empty($row['enum'])){
	
					$out.=$this->_select("value[$i]",$row['enum'],$row['value']);
	
				}else{
	
					$out.=$e->edit($i,0);
	
				}
	
				if(!empty($row['function'])&&class_exists($row['function'])){
	
					$f=new $row['function'];
					$f->setId($i);
					$out.=$f->display();
				}
				$out.="<br />".nl2br($row['description']);
				$out.="\r\n<br /><br />\r\n";
				$i++;
			}
			$hidden='style="display:none"';
			$out.='</div>';
		}
		*/
		$out.="<input type=\"hidden\" name=\"save\" value=\"save\" />\r\n";
		$out.="<input type=\"submit\" value=\"Save\" /></form>";
		$out.="</div></div>";
		return $out;
	}

	private function _select($name, $values, $value)
	{
  		$r='<select name="'.$name.'">';
  		foreach(explode(',',$values) as $v){
	  		if($value==$v){
	  			$r.='<option value="'.$v.'" selected="selected">'.$v.'</option>';
	  		}else{
	  			$r.='<option value="'.$v.'">'.$v.'</option>';
	  		}
  		}
	  	$r.='</select>';
	  	return $r;
	}
}
?>